


import json
from framework.frontend.speech_client import fallback_tts
import sounddevice as sd
import soundfile as sf
from audioBot.cmd_executors.llm_serve import llm
from tool.config.config import get_config
from webui.views.llm_serve.llm_serve import get_context_with_doctype

def say_sth(text):
    fallback_tts(text,"./tmp.wav")
    data, samplerate = sf.read("./tmp.wav", dtype='float32')  # 读取音频数据
    sd.play(data, samplerate)  # 播放
    sd.wait()  # 等待播放结束

def summerize_executor(recorder):
    context_str = get_context_with_doctype("helloyutao", "", doctype="wrongQuestion")
    TEMPLATE = """
以下是一位学生的错题
<context>
{context}
</context>
请根据上述题目总结该学生的学习状态
    """
    if context_str:
        print(f"Context for summarization: {context_str}")
        llm.acceptQuery(TEMPLATE.format(context=context_str), get_config("config.toml")["LLM"]["API_PORT"], model=get_config("config.toml")["LLM"]["MODEL_NAME"])
        summary = ""
        for chunk in llm.stream():
            chunk = json.loads(chunk)["message"]["content"]
            print(chunk, end="", flush=True)
            say_sth(chunk)
    else:
        print("No context available for summarization.")
    say_sth("向量库中尚不存在您的状态总结")